#!/bin/bash
#功能函数
funcs=`which functions.sh`
if [ "$funcs" != "" ] ;then
    echo "funcs=$funcs"
    . $funcs
else
    funcs=./functions.sh
    if [ "$funcs" != "" ] ;then
	echo "funcs=$funcs"
	. $funcs
    else
	echo "./functions.sh not exist!"
	return
    fi
fi
default_project_name="TDXXXX,PDXXXX"
SVNCMD=`which svn.shc`

log_f_dir=$HOME/svnlog
log_frame_V_all_file=$log_f_dir/${USER}-frame_svn-log-v-all.txt
log_app_V_all_file=$log_f_dir/${USER}-app_svn-log-v-all.txt
ReleaseNotesD=$HOME/releasenotes/

function showHelp()
{
  echoTextRed "参数：
	--for
	    mergelog	：生成项目产品同步的log文件
	    swversion	：按软件版本取
	--help|-help	：显示帮助
其他：
	脚本执行后，会分类保存svn修改记录，如果要查看更详细的信息，请查看这些分类文件"
}

function print_vars()
{
	echoTextBlue "selProject=$selProject"
	echoTextBlue "repoRootURL=$repoRootURL"
	echoTextBlue "productPath=$productPath"
	echoTextBlue "trunkPath=$trunkPath"
	echoTextBlue "driverPath=$driverPath"
	echoTextBlue "modemPath=$modemPath"
	echoTextBlue "appRepoRootURL=$appRepoRootURL"
	echoTextBlue "appPackagePath=$appPackagePath"
	echoTextBlue "appproductPath=$appproductPath"
	echoTextBlue "framework_trunk_start_version=$framework_trunk_start_version"
	echoTextBlue "app_trunk_start_version=$app_trunk_start_version"
	echoTextBlue "framework_trunk_to_product_version=$framework_trunk_to_product_version"
	echoTextBlue "app_trunk_to_product_version=$app_trunk_to_product_version"
	echoTextBlue "framework_product_start_version=$framework_product_start_version"
	echoTextBlue "app_product_start_version=$app_product_start_version"
}

function gen_log_file_from_url()
{
	local url=$1
	local svn_version_range=$2
	local output_file=$3
	echo "url=$url"
	rm -rf $3
	echo "svn log -v -r $svn_version_range $url >$output_file"
	svn log -v -r $svn_version_range $url >$output_file
}

function filter_versions()
{
  all_versions=$1
  xxx_versions=$2
  yyy_versions=
  for v in $all_versions
  do
    result=`echo "$xxx_versions" |grep $v -w`
    if [ -n "$result" ]; then
      yyy_versions="$yyy_versions $v"
    fi
  done
  echo "$yyy_versions"
}

function parse_svn_log_by_author()
{
  local f_in=$1
  local l_keyword=$2
  local f_out=$3
  cat $f_in | sed -n "/$l_keyword/,/----/p" >$f_out
}

function get_release_nostes()
{
  local f_in=$1
  local f_out=$2
  local project_name=$3
  local l_version=
  local l_keyword=
  if [ -n "$project_name" ]; then
    project_name="`echo $project_name |tr ',' ' '`"
  fi
  for n in $project_name
  do
    if [ -n "$l_keyword" ]; then
      l_keyword="$l_keyword|$n"
    else
      l_keyword=$n
    fi
  done

  l_version=`cat $f_in |grep "^r[0-9]" |awk '{print $1}'|tr -d 'r'`
  for v in $l_version 
  do 
    if [ -n "$l_keyword" ]; then
		#echo "cat $f_in | sed -n "/r$v/,/----/p" | grep -v "^ " | grep -E "$l_keyword""
        result=`cat $f_in | sed -n "/r$v/,/----/p" | grep -v "^ " | grep -E "$l_keyword"`
		#echo "result=$result"
        if [ -n "$result" ]; then
          cat $f_in | sed -n "/r$v/,/----/p" >> $f_out
        fi
    else
      cat $f_in | sed -n "/r$v/,/----/p" >> $f_out
    fi
  done
}

function process_log_info()
{
	local svn_log_file=$1
	local merge_versions=$2
	local project_name=$3
	local type=$4
	local unmerge_versions=	
	ReleaseNotesF=$ReleaseNotesD/${type}_ReleaseNotes.txt
	MergeInfoF=$ReleaseNotesD/${type}_MergeInfos.txt
	MergeNotesF=$ReleaseNotesD/${type}_MergeNotes.txt

	all_authors=`cat $svn_log_file | grep  "^r[0-9]" | awk -F '|' '{print $2}' |sort |uniq`
	all_versions=`cat $svn_log_file  | grep  "^r[0-9]" |tr "r" " "| awk -F '|' '{print $1}' |sort |uniq`


	for v in $all_versions
	do
	  result=`echo $merge_versions|grep $v -w`
	  if [ -z "$result" ]; then
			unmerge_versions="$unmerge_versions $v"
	  fi 
	done
	unmerge_versions=`echo $unmerge_versions | tr " " ","`
	if test -d $ReleaseNotesD/${type}/authors/;then
		rm -rf $ReleaseNotesD/${type}/authors/*
	else
		mkdir $ReleaseNotesD/${type}/authors/ -pv
	fi

	#拷贝ReleaseNotes文件
	rm $ReleaseNotesF -rf; touch $ReleaseNotesF
	get_release_nostes  $svn_log_file $ReleaseNotesF "$project_name"
	#处理每个提交者的信息
	for a in $all_authors
	do
		author_versions=`cat $svn_log_file |grep $a | grep "^r" | awk -F '|' '{print $1}' | tr 'r' ' ' | tr '\n' ' ' | sort | uniq`
		author_merge_versions=`filter_versions "$author_versions" "$merge_versions"`
		author_unmerge_versions=`filter_versions "$author_versions" "$unmerge_versions"`
		##输出到屏幕
		echoTextBlue  "提交作者: $a --- authors/$a.txt 该作者log汇总"
		echoTextCyan  "所有版本：$author_versions"
		echoTextGreen "已合并:  $author_merge_versions"
		echoTextRed   "未合并:  $author_unmerge_versions"
		#输出到文件
		echo  "提交作者: $a --- authors/$a.txt 该作者log汇总">>$MergeInfoF
		echo  "所有版本：$author_versions">>$MergeInfoF
		echo  "已合并:  $author_merge_versions">>$MergeInfoF
		echo  "未合并:  $author_unmerge_versions">>$MergeInfoF

		if [ -n "$author_unmerge_versions" ]; then
			echo  "$a 未合并的版本:">>$MergeNotesF
			echo  "$author_unmerge_versions">>$MergeNotesF
		fi

		parse_svn_log_by_author $svn_log_file  $a $ReleaseNotesD/${type}/authors/$a.log
	done
	echo "合并的版本:$merge_versions"
	echo "未合并版本:$unmerge_versions"


	echoTextBlue  "分类保存svn修改记录:"
	echoTextGreen "
	$ReleaseNotesF
	$MergeInfoF
	$MergeNotesF
	$ReleaseNotesD/${type}/authors/
	"
}

function mergelog()
{
	rm -rvf $log_f_dir
	mkdir -pv $log_f_dir
	rm -rvf $ReleaseNotesD
	mkdir -pv $ReleaseNotesD

	InitVars
	print_vars
	#生成主线所有svnlog信息的svnlog文件
	gen_log_file_from_url ${repoRootURL}/${trunkPath}  ${framework_trunk_to_product_version}:HEAD $log_frame_V_all_file
	gen_log_file_from_url ${appRepoRootURL}/${appPackagePath} ${app_trunk_to_product_version}:HEAD $log_app_V_all_file

	#获取产品线所有已经合并过的svn版本
	all_framework_product_merged_svn_version="`process_log_file ${repoRootURL}/${productPath} ${framework_product_start_version}:HEAD $selProject`"
	echoTextGreen "all_framework_product_merged_svn_version=$all_framework_product_merged_svn_version"
	all_app_product_merged_svn_version="`process_log_file ${appRepoRootURL}/${appproductPath} ${app_product_start_version}:HEAD $selProject`"
	echoTextGreen "all_app_product_merged_svn_version=$all_app_product_merged_svn_version"
	#生成信息到文件中
	process_log_info $log_frame_V_all_file $all_framework_product_merged_svn_version $selProject "frame"
	process_log_info $log_app_V_all_file $all_app_product_merged_svn_version $selProject "app"
}

function pull_product_svn_version()
{
	echo "function------------------>>>pull_product_svn_version"
}

#脚本入口
while test $# != 0
do
    case $1 in
	-r)
	    shift
	    version=$1
	    ;; 
    -t)
	    shift
	    date=$1
	    ;; 
    -u)
	    shift
	    author=$1
	    ;; 
   	-m)
	    shift
	    mode=$1
	    ;;
   	-p)
	    shift
	    project_name=$1
	    ;;
   	--for)
	    shift
	    for_use=$1
	    ;;
	--url)
	    shift
	    url=$1
	    ;;
	--help | --h)
		shift
		showHelp
		;;
    *)
	   work_copy=$1
	   ;;
    esac
       shift
done

menu_list="
mergelog
pull_product_svn_version
"
echoTextBlue "Please select"
result=`sh_menu_select "$menu_list"`
echo "${result}"
${result}
